@extends('cms/system/admin/template/theme_default/common')
@section('page-body')
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <div class="layui-form layui-form-pane" id="office-disburse-form" lay-filter="office-disburse-form">
                    <div class="layui-form-item">
                        <label class="layui-form-label">所属应用</label>
                        @if(isset($app) && $app)
                            <div class="layui-input-block">
                                <input id="app-id" class="layui-input" type="hidden" name="app_id" value="{{$app['id'] ?? 0}}" />
                                <input class="layui-input" type="text" value="{{$app['name'] ?? ''}}" placeholder="请选择所属应用" autocomplete="off" lay-verify="required" required readonly />
                            </div>
                        @else
                            <div class="layui-input-block">
                                <input id="app-id" class="layui-input" type="hidden" name="app_id" value="{{$data['app_id'] ?? 0}}" />
                                <input id="app-select" class="layui-input" type="text" value="{{$data['app']['name'] ?? ''}}" placeholder="请选择所属应用" autocomplete="off" lay-verify="required" required readonly />
                            </div>
                        @endif
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">所属项目</label>
                        @if(isset($project) && $project)
                            <div class="layui-input-block">
                                <input id="project-id" class="layui-input" type="hidden" name="project_id" value="{{$project['id'] ?? 0}}" />
                                <input class="layui-input" type="text" value="{{$project['name'] ?? ''}}" placeholder="请选择所属项目" autocomplete="off" lay-verify="required" required readonly />
                            </div>
                        @else
                            <div class="layui-input-block">
                                <input id="project-id" class="layui-input" type="hidden" name="project_id" value="{{$data['project_id'] ?? 0}}" />
                                <input id="project-select" class="layui-input" type="text" value="{{$data['project']['name'] ?? ''}}" placeholder="请选择所属项目" autocomplete="off" lay-verify="required" required readonly />
                            </div>
                        @endif
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">支出时间</label>
                        <div class="layui-input-block">
                            <input class="layui-input" id="date_at" type="hidden" name="date_at" value="" />
                            <input id="select-date" class="layui-input" type="text" value="" placeholder="年-月-日" autocomplete="off" lay-verify="required" required />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">支出金额</label>
                        <div class="layui-input-block">
                            <input class="layui-input" type="text" name="amount" value="" placeholder="回款金额，单位元" autocomplete="off" lay-verify="required" required />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">报销凭证</label>
                        <div class="layui-input-block">
                            <input class="layui-input" type="text" name="evidence" value="" placeholder="报销或支出凭据" autocomplete="off" />
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">备注说明</label>
                        <div class="layui-input-block">
                            <textarea class="layui-textarea" name="excerpt" placeholder="请输入内容"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-block">
                            <input class="layui-btn" type="button" value="添加" id="submit-create" lay-submit lay-filter="submit-create" />
                            <input class="layui-btn" type="button" value="更新" id="submit-update" lay-submit lay-filter="submit-update" />
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script type="text/javascript">
        layui.config({
            base: '/layui-admin/'
        }).extend({
            index: 'lib/index'
        }).use(['index', 'system'], function () {
            // region 渲染时间控件 +++++
            layui.laydate.render({
                elem: '#select-date',
                type: 'date',
                done: function (value, date) {
                    // console.log(value, start, finish)
                    layui.jquery('#date_at').val(date.year + '-' + date.month + '-' + date.date);
                }
            });
            // endregion

            // 选择应用
            layui.$('#app-select').on('click', function () {
                // region 处理已选中 +++++
                var selectValueDom = layui.$('#app-id');
                var selectValue = selectValueDom.val();
                var selectTextDom = layui.$('#app-select');
                // endregion

                var selectLayer = top.layui.layer.open({
                    type: 2,
                    title: '选择',
                    content: layui.setter.mvcLink + '/office/admin_app/select?select_multi=0&select_value=' + selectValue,
                    maxmin: true,
                    area: '500px',
                    maxHeight: '500px',
                    btn: ['确定', '取消'],
                    yes: function (index, parentLayer) {
                        console.log(index, selectLayer)
                        // 取得iframe窗口
                        // var iframe = window[parentLayer.find('iframe')[0]['name']];
                        var iframe = top.layui.$('#layui-layer-iframe' + index)[0];
                        var selectResult = iframe.contentWindow.selectConfirm();
                        if (selectResult.data.length != 1) {
                            top.layui.layer.msg('请正确选择所属题库');
                            return false;
                        }
                        var selectText = '';
                        // 清空已有值，避免再次选择重复问题
                        selectValue = '';
                        layui.$.each(selectResult.data, function (data_k, data_v) {
                            if (data_k > 0) {
                                selectValue += ',';
                                selectText += ',';
                            }
                            selectValue += data_v.id;
                            selectText += data_v.id + '#' + data_v.name;
                        });
                        // 设置值
                        selectValueDom.val(selectValue);
                        selectTextDom.val(selectText);
                        top.layui.layer.close(index);
                    }
                });
            });

            // 选择项目
            layui.$('#project-select').on('click', function () {
                // region 处理已选中 +++++
                var selectValueElement = layui.$('#project-id');
                var selectValue = selectValueElement.val();
                var selectTextElement = layui.$('#project-select');
                var selectText = '';
                // endregion
                var selectLayer = layui.system.iframeLayer({
                    sourceWindow: window,
                    targetName: 'top',
                    title: '选择',
                    content: layui.setter.mvcLink + '/office/admin_project/select?app_id={{$app['id'] ?? 0}}&select_multi=0&select_value=' + selectValue,
                    btn: ['确定', '取消'],
                    yes: function (layerIndex, layerWindow) {
                        // 取得iframe窗口
                        var iframe = layerWindow.find('iframe')[0];
                        var selectResult = iframe.contentWindow.selectConfirm();
                        if (selectResult.data.length != 1) {
                            layui.layer.msg('请正确选择配置');
                            return false;
                        }
                        selectText = selectResult.data[0].name;
                        // 清空已有值，避免再次选择重复问题
                        selectValue = selectResult.data[0].id;
                        // 设置值
                        selectValueElement.val(selectValue);
                        selectTextElement.val(selectText);
                    }
                });
            });
            // 创建
            layui.form.on('submit(submit-create)', function (data) {
                // 获取提交的字段
                var formField = data.field;
                // formField.content = layui.layedit.getContent(layuiEdit);
                console.log('表单字段：', formField);
                // ajax提交
                layui.$.ajax({
                    url: layui.setter.apiLink + '/office/admin_disburse/create?app_id={{$app['id'] ?? 0}}',
                    type: 'POST',
                    data: formField,
                    success: function (result) {
                        if (result.code != 1) {
                            layui.layer.alert(result.msg);
                            return false;
                        }
                        // 刷新列表
                        window.SOURCE_WINDOW.layui.table.reload('office-disburse-list');
                    }
                });
            });
            // 更新
            layui.form.on('submit(submit-update)', function (data) {
                // 获取提交的字段
                var formField = data.field;
                console.log('表单字段：', formField);
                // ajax提交
                layui.$.ajax({
                    url: layui.setter.apiLink + '/office/admin_disburse/update?app_id={{$app['id'] ?? 0}}&id={{$data['id'] ?? 0}}',
                    type: 'POST',
                    data: formField,
                    success: function (result) {
                        if (result.code != 1) {
                            layui.layer.alert(result.msg);
                            return false;
                        }
                        // 刷新列表
                        window.SOURCE_WINDOW.layui.table.reload('office-disburse-list');
                    }
                });
            });
        });
    </script>
@endsection